﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Crm;
using PeiYue.IServices.ERP;
using PeiYue.Model;
using PeiYue.Model.Dto;

namespace PeiYue.Api.Controllers.Crm
{
    /// <summary>
    /// 门店管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class ShopController : ControllerBase
    {
        readonly IShopServices _shopServices;
        readonly IImportShopServices _importShopServices;
        readonly IUser _user;
        readonly IMapper _mapper;
        public ShopController(IShopServices shopServices, IImportShopServices importShopServices, IUser user, IMapper mapper)
        {
            _shopServices = shopServices;
            _importShopServices = importShopServices;
            _user = user;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取门店下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("{customerId}")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public async Task<MessageModel<List<SelectItemDto>>> GetSelectItem(long customerId)
        {
            var data = await _shopServices.Query(p =>p.customer_id==customerId && p.status == 1);
            var result = data.Select(p => new SelectItemDto { Id = p.id, Name = p.names }).ToList();
            return new MessageModel<List<SelectItemDto>>()
            {
                msg = "获取成功",
                success = true,
                response = result
            };
        }
        /// <summary>
        /// 获取门店下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        [AllowAnonymous]
        public async Task<MessageModel<List<SelectItemDto>>> GetSelectItem()
        {
            var data = await _shopServices.Query(p => p.status == 1);
            var result = data.OrderBy(p=>p.names).Select(p => new SelectItemDto { Id = p.id, Name = p.namec }).ToList();
            return new MessageModel<List<SelectItemDto>>()
            {
                msg = "获取成功",
                success = true,
                response = result
            };
        }
        /// <summary>
        /// 把ERP门店数据导入OMS
        /// </summary>
        /// <param name="isAll">true全部导入,false增量导入</param>
        /// <returns></returns>
        [HttpGet()]
        [AllowAnonymous]
        public async Task<MessageModel<string>> ImportShopFromERP([FromQuery]bool isAll=false)
        {
            await _importShopServices.ImportShopFromERP(isAll);
            return new MessageModel<string>()
            {
                msg = "导入成功",
                success = true,
            };
        }
    }
}